package com.hundsun.fcp.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.TradeRequest;
import com.hundsun.fcp.common.utils.MD5;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.openapi.trade.bean.RequestLoginBean;
import com.hundsun.fcp.widget.EditTextWithClear;

public class LoginActivity extends BaseActivity implements OnClickListener{
	private EditTextWithClear mMobile;
	private EditTextWithClear mPwd;
	private Button mLogin;
	private TextView mForgetPwd;
	private String strMobile;
	private String strPwd;
	
	private RequestLoginBean loginParam;
	
	@Override
	protected int getContentLayout() {
		return R.layout.fcp_mine_login;
	}
	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_btn_login);
		mHeaderMenu.setRightText(R.string.mine_signup);
		mHeaderMenu.setRightClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Redirect.startSignUp(LoginActivity.this);
			}
		});
	}
	@Override
	public void initView() {
		super.initView();
		loginParam = new RequestLoginBean() ;
		mMobile = (EditTextWithClear) findViewById(R.id.id_mobile_modify_ipt);
		mPwd = (EditTextWithClear) findViewById(R.id.id_pwd_ipt);
		mLogin = (Button) findViewById(R.id.btn_submit);
		mForgetPwd = (TextView) findViewById(R.id.tv_forget_pwd);
		mLogin.setOnClickListener(this);
		mForgetPwd.setOnClickListener(this);
		mMobile.setText(FcpUserManager.getInstance().getUserMobile()) ;
		mMobile.addTextChangedListener(mTextWatcher);
		mPwd.addTextChangedListener(mTextWatcher);
	}
	@Override
	public void onClick(View v) {
		if(v == mLogin){
			//已经判定两个输入框不为空,strMobile和strPwd可直接使用
			strMobile = mMobile.getText().toString().trim();
			strPwd = mPwd.getText().toString().trim();
			//doLogin(strMobile,strPwd);
			loginParam.mobile = strMobile ;
			loginParam.password = MD5.getMD5Str(strPwd) ;
			
			TradeRequest.doLogin(this, loginParam, true, true) ;
			
		}else if(v == mForgetPwd){
			Redirect.startResetLoginPwd(LoginActivity.this);
		}
	}
	/**
	 * EditText监听器
	 */
	private TextWatcher mTextWatcher = new TextWatcher() {
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}
		
		public void afterTextChanged(Editable s) {
			strMobile = mMobile.getText().toString().trim();
			strPwd = mPwd.getText().toString().trim();
			if(StringUtils.isEmpty(strMobile)||StringUtils.isEmpty(strPwd) ){
				mLogin.setEnabled(false) ;
				mLogin.setBackgroundResource(R.drawable.btn_grey_unable) ;
			}else{
				mLogin.setEnabled(true) ;
				mLogin.setBackgroundResource(R.drawable.btn_red_normal) ;
			}
			
		}
	};
	
	@Override
	public void afterLogin() {
		setResult(RESULT_OK) ;
		LoginActivity.this.finish() ;
	};

}
